package com.hr.rbac.service;

import java.util.List;

import org.junit.Test;

import com.hr.rbac.dao.Impl.FunctionDaoImpl;
import com.hr.rbac.dao.Impl.PrivilegeDaoImpl;
import com.hr.rbac.dao.Impl.RoleDaoImpl;
import com.hr.rbac.domain.Function;
import com.hr.rbac.domain.Privilege;
import com.hr.rbac.domain.Role;

public class RBACService {

	private RoleDaoImpl roleDaoImpl = new RoleDaoImpl();
	
	private PrivilegeDaoImpl privilegeDaoImpl = new PrivilegeDaoImpl();
	
	private FunctionDaoImpl functionDaoImpl = new FunctionDaoImpl();
	
	public Role findByUserID(String userID) {
		Role role = roleDaoImpl.findByRoleUserId(userID);
		if(role!=null) {
			List<Privilege> privileges = privilegeDaoImpl.findByRoleId(roleDaoImpl.findByRoleUserId(userID).getRole_ID());
			for(int i=0;i<privileges.size();i++) {
				List<Function> functions = functionDaoImpl.findByPrivilegeID(privileges.get(i));
				privileges.get(i).setFunctions(functions);
			}
			role.setPrivileges(privileges);
			return role;
		} else {
			return null;
		}
		
		
	}
	
	@Test
	public void test() {
		//查询用户组
		System.out.println("当前用户组：");
		System.out.println(roleDaoImpl.findByRoleId("61A8607D898142AA9C0D9567DD4467CA"));
		//查询当前用户所在用户组所用有的权限
		System.out.println("当前用户组所拥有的权限：");
		List<Privilege> privileges = privilegeDaoImpl.findByRoleId(roleDaoImpl.findByRoleUserId("dbb80172648a445d82579dbda5b74c92").getRole_ID());
		for(Privilege privilege : privileges) {
			System.out.println(privilege);
			System.out.println("\t||");
			List<Function> functions = functionDaoImpl.findByPrivilegeID(privilege.getPrivilege_id());
			for(Function function : functions) {
				System.out.println("\t|=="+function);
			}
		}
	}
}
